package com.cheer.ba.presenter.impl;

import android.app.Dialog;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.cache.preference.EntityCache;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.ReturnCode;
import com.cheer.ba.http.BaseCallback;
import com.cheer.ba.http.CallManager;
import com.cheer.ba.http.HandleRetCode;
import com.cheer.ba.http.Http;
import com.cheer.ba.http.request.APIService;
import com.cheer.ba.model.CheerCircleModel;
import com.cheer.ba.model.CommentModel;
import com.cheer.ba.model.ResponseData;
import com.cheer.ba.model.TagModel;
import com.cheer.ba.presenter.GeneralPresenter;
import com.cheer.ba.utils.NetUtils;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.IViewController;
import com.cheer.ba.view.dialog.DialogUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Response;

/**
 * 评论处理器
 *
 * @author HQ
 *         created at 2017/3/3 0003 15:50
 */
public class CommentPresenter extends GeneralPresenter {

    private Context mContext;
    private IViewController mView;
    private APIService mUserService;
    private EntityCache<CheerCircleModel> mCheerCircleCache;
    private List<CheerCircleModel> mSaveCacheList = new ArrayList<CheerCircleModel>();

    //记录当前分页下标
    private int mRecordPage = 0;
    private int mIndex = 0;

    //数据是否加载失败
    private boolean isDataError = false;

    @Override
    public void attachView(IViewController view) {
        this.mView = view;
        mContext = getContext(view);
        mUserService = Http.getUserService(mContext);
        mCheerCircleCache = new EntityCache<CheerCircleModel>(mContext, CheerCircleModel.class, "");
    }

    /**
     * 评论列表界面
     *
     * @param sourceType 1:资讯2订单 5 柴油批发的评论列表
     * @param sourceId   评论对象ID
     */
    public void getComments(final boolean loadMore, boolean isShowLoad, String sourceType, String sourceId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<CommentModel>>> call = mUserService.getUserComments(1508, Session.getUserId(), sourceType, sourceId, mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CommentModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CommentModel>>> call, Response<ResponseData<List<CommentModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CommentModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CommentModel> comments = res.getData();
                        if (null != comments) {
                            if (comments.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(comments, loadMore, ConstantTag.COMMENT_LIST_SUCCESS);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CommentModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 动态列表，加油圈
     */
    public void getDynamicList(final boolean loadMore, boolean isShowLoad, int size) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += size;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<CheerCircleModel>>> call = mUserService.getDynamicList(1508, Session.getUserId(), mRecordPage, size);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CheerCircleModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CheerCircleModel>>> call, Response<ResponseData<List<CheerCircleModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CheerCircleModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CheerCircleModel> comments = res.getData();
                        List<CheerCircleModel> mCheerCircles = new ArrayList<CheerCircleModel>();
                        if (null != comments) {
                            if (comments.isEmpty()) {
                                isDataError = true;
                                if (!loadMore) {
                                    mSaveCacheList.clear();
                                }
                            } else {
                                for (CheerCircleModel model : comments) {
                                    if (model.dynamicStatus != 0) {
                                        mCheerCircles.add(model);
                                    }
                                }
                                if (mCheerCircles.isEmpty()) {
                                    isDataError = true;
                                    if (!loadMore) {
                                        mSaveCacheList.clear();
                                    }
                                } else {
                                    if (!loadMore) {
                                        mSaveCacheList.clear();
                                    }
                                    mSaveCacheList.addAll(mCheerCircles);
                                    isDataError = false;
                                }
                            }
                            if (mCheerCircles.isEmpty()) {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "", loadMore);
                            } else {
                                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                                mView.updateViewWithLoadMore(mCheerCircles, loadMore, ConstantTag.COMMENT_LIST_SUCCESS);
                            }
                            try {
                                mCheerCircleCache.putListEntity(mSaveCacheList);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CheerCircleModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 标签获取动态列表，加油圈
     */
    public void getDynamicListTag(final boolean loadMore, boolean isShowLoad, String tagId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<CheerCircleModel>>> call = mUserService.getDynamicListTag(1508, Session.getUserId(), tagId, mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CheerCircleModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CheerCircleModel>>> call, Response<ResponseData<List<CheerCircleModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CheerCircleModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CheerCircleModel> comments = res.getData();
                        List<CheerCircleModel> mCheerCircles = new ArrayList<CheerCircleModel>();
                        if (null != comments) {
                            if (comments.isEmpty()) {
                                isDataError = true;
                            } else {
                                for (CheerCircleModel model : comments) {
                                    if (model.dynamicStatus != 0) {
                                        mCheerCircles.add(model);
                                    }
                                }
                                if (mCheerCircles.isEmpty()) {
                                    isDataError = true;
                                } else {
                                    isDataError = false;
                                }
                            }
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(mCheerCircles, loadMore, ConstantTag.COMMENT_LIST_SUCCESS);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CheerCircleModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, true, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 回复列表界面
     *
     * @param commentId 评论id
     */
    public void getReplyList(final boolean loadMore, boolean isShowLoad, String commentId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, "", loadMore);
            return;
        }
        if (!loadMore) {
            isDataError = false;
            mRecordPage = 0;
        } else {
            if (isDataError) {
                mRecordPage = mIndex;
            } else {
                mRecordPage += Constants.PAGESIZE;
            }
        }
        mIndex = mRecordPage;
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, "", loadMore);
        }
        Call<ResponseData<List<CommentModel>>> call = mUserService.getReplyList(1508, Session.getUserId(), commentId, mRecordPage, Constants.PAGESIZE);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CommentModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CommentModel>>> call, Response<ResponseData<List<CommentModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CommentModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CommentModel> comments = res.getData();
                        if (null != comments) {
                            if (comments.isEmpty()) {
                                isDataError = true;
                            } else {
                                isDataError = false;
                            }
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "", loadMore);
                            mView.updateViewWithLoadMore(comments, loadMore, ConstantTag.COMMENT_LIST_SUCCESS);
                            return;
                        }
                    }
                }
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }

            @Override
            public void onFailure(Call<ResponseData<List<CommentModel>>> call, Throwable t) {
                super.onFailure(call, t);
                isDataError = true;
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "", loadMore);
            }
        });
    }

    /**
     * 删除评论或者回复
     *
     * @param commentId 评论或回复ID
     */
    public void delMyComment(boolean isShowLoad, String commentId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.delMyComment(1508, Session.getUserId(), commentId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.DEL_COMMENT_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "删除失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "删除失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "删除失败");
            }
        });
    }

    /**
     * 动态删除
     */
    public void delDynamic(boolean isShowLoad, String dynamicId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.delDynamic(1508, Session.getUserId(), dynamicId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.DEL_DYNAMIC);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "删除失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "删除失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "删除失败");
            }
        });
    }

    /**
     * 用户操作对象
     *
     * @param operationType 1:浏览2下载3收藏4点赞5转发
     * @param sourceType    1:资讯2订单12动态
     * @param sourceId      对象唯一标识
     */
    public void userOperation(boolean isShowLoad, String operationType, String sourceType, String sourceId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.userOperation(1508, Session.getUserId(), operationType, sourceType, sourceId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.LIKE_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "点赞失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "点赞失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "点赞失败");
            }
        });
    }

    /**
     * 用户取消点赞
     *
     * @param sourceId
     */
    public void userDelOperation(boolean isShowLoad, String operationType, String sourceType, String sourceId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.userDelOperation(1508, Session.getUserId(), operationType, sourceType, sourceId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(res.getRet(), ConstantTag.DEL_LIKE_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "操作失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }
        });
    }

    /**
     * 获得用户点赞列表
     *
     * @param sourceType 1:资讯2订单
     * @param sourceId   对象唯一标识
     */
    public void getUserZanList(boolean isShowLoad, String sourceType, String sourceId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData<List<CommentModel>>> call = mUserService.getZanList(1508, Session.getUserId(), sourceType, sourceId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<CommentModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<CommentModel>>> call, Response<ResponseData<List<CommentModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<CommentModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<CommentModel> zanList = res.getData();
                        if (null != zanList && !zanList.isEmpty()) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                            mView.updateViewWithTag(zanList, ConstantTag.GET_LIKES_SUCCESS);
                        } else {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "");
                        }
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "获取点赞列表失败");
            }

            @Override
            public void onFailure(Call<ResponseData<List<CommentModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "获取点赞列表失败");
            }
        });
    }

    /**
     * 获得标签列表
     */
    public void getTagList(boolean isShowLoad, String tagType) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData<List<TagModel>>> call = mUserService.getTagList(1508, tagType);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<List<TagModel>>>() {
            @Override
            public void onResponse(Call<ResponseData<List<TagModel>>> call, Response<ResponseData<List<TagModel>>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<List<TagModel>> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        List<TagModel> tagList = res.getData();
                        if (null != tagList && !tagList.isEmpty()) {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                            mView.updateViewWithTag(tagList, ConstantTag.GET_TAGS_SUCCESS);
                        } else {
                            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "");
                        }
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "获取标签列表失败");
            }

            @Override
            public void onFailure(Call<ResponseData<List<TagModel>>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "获取标签列表失败");
            }
        });
    }

    /**
     * 动态发布
     *
     * @param dynamicType 1:图文2音频3视频4链接
     */
    public void publishDynamic(boolean isShowLoad, String title, String tagsId, String picJson, String dynamicType, String location) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData> call = mUserService.publishDynamic(1508, Session.getUserId(), title, tagsId, picJson, dynamicType, location);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData>() {
            @Override
            public void onResponse(Call<ResponseData> call, Response<ResponseData> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag("", ConstantTag.PUBLISH_DYNAMIC);
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, CodeStatus.Gegeneral.DATA_SUCCESS_NULL, "");
                        HandleRetCode.handler(mContext, res, true, "发布失败");
                    }
                    return;
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "发布失败");
            }

            @Override
            public void onFailure(Call<ResponseData> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "发布失败");
            }
        });
    }

    /**
     * 用户评论
     *
     * @param context    评论内容
     * @param sourceType 1:资讯2订单
     * @param sourceId   对象唯一标识
     * @param picJson    图片
     */
    public void userComment(boolean isShowLoad, String context, String sourceType, String sourceId, String picJson) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData<CommentModel>> call = mUserService.userComment(1508, Session.getUserId(), context, sourceType, sourceId, picJson);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<CommentModel>>() {
            @Override
            public void onResponse(Call<ResponseData<CommentModel>> call, Response<ResponseData<CommentModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<CommentModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        CommentModel model = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(model, ConstantTag.COMMENT_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "操作失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }

            @Override
            public void onFailure(Call<ResponseData<CommentModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }
        });
    }

    /**
     * 用户回复
     *
     * @param context        回复内容
     * @param sourceType     1:资讯2订单
     * @param sourceId       对象唯一标识
     * @param replyCustId    回复哪个用户
     * @param replyCommentId 回复哪条评论
     */
    public void userReply(boolean isShowLoad, String context, String sourceType, String sourceId, String replyCustId, String replyCommentId) {
        if (!NetUtils.isNetworkConnected(mContext)) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_NET, CodeStatus.Gegeneral.ERROR_NET, mContext.getString(R.string.no_net_work));
            return;
        }
        if (isShowLoad) {
            mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_LOADING, CodeStatus.Gegeneral.DATA_LOADING, mContext.getString(R.string.data_loading));
        }
        Call<ResponseData<CommentModel>> call = mUserService.userReply(1508, Session.getUserId(), context, sourceType, sourceId, replyCustId, replyCommentId);
        CallManager.add(call);
        call.enqueue(new BaseCallback<ResponseData<CommentModel>>() {
            @Override
            public void onResponse(Call<ResponseData<CommentModel>> call, Response<ResponseData<CommentModel>> response) {
                super.onResponse(call, response);
                if (response.isSuccessful()) {
                    ResponseData<CommentModel> res = response.body();
                    if (res.getRet() == ReturnCode.SUCCESS) {
                        CommentModel model = res.getData();
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, CodeStatus.Gegeneral.DATA_SUCCESS_FULL, "");
                        mView.updateViewWithTag(model, ConstantTag.REPLY_SUCCESS);
                        return;
                    } else {
                        mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "");
                        HandleRetCode.handler(mContext, res, true, "操作失败");
                        return;
                    }
                }
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }

            @Override
            public void onFailure(Call<ResponseData<CommentModel>> call, Throwable t) {
                super.onFailure(call, t);
                mView.onLoadingStatus(mView, false, CodeStatus.Gegeneral.ERROR_DATA, CodeStatus.Gegeneral.ERROR_DATA, "操作失败");
            }
        });
    }

    public PopupWindow mPopup;
    //评论或者回复弹出框
    private EditText mEtvComment;

    /**
     * 弹出评论框或者回复
     *
     * @param flag 1 评论 2 回复
     */
    public void showCommentPop(final View mRootView, final String flag, String editext, final boolean isFrg) {
        LayoutInflater inflater = LayoutInflater.from(mRootView.getContext());
        // 引入窗口配置文件
        final View view = inflater.inflate(R.layout.pop_show_comment_edit, null);
        mEtvComment = (EditText) view.findViewById(R.id.etv_comment);
        final TextView mBtnSend = (TextView) view.findViewById(R.id.id_btn_commit);
        mEtvComment.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                showCopyDialog();
                return false;
            }
        });
        mBtnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = mEtvComment.getText().toString();
                mEtvComment.setText("");
                view.setVisibility(View.GONE);
                if (null != mPopup) {
                    mPopup.dismiss();
                }
                mView.updateViewWithTag(content, flag);
            }
        });
        mBtnSend.setEnabled(false);
        mEtvComment.setHint(editext);
        mEtvComment.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                String mText = s + "";
                if (mText.trim().length() > 0) {
                    mBtnSend.setEnabled(true);
                } else {
                    mBtnSend.setEnabled(false);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        // 显示窗口
        // 创建PopupWindow对象
        mPopup = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, false);
        // 需要设置一下此参数，点击外边可消失
        mPopup.setBackgroundDrawable(new BitmapDrawable());
        //设置点击窗口外边窗口消失
        mPopup.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        mPopup.setFocusable(true);
        //防止虚拟软键盘被弹出菜单遮住
        mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        //        pop.showAsDropDown(v);
        mPopup.showAtLocation(mRootView, Gravity.BOTTOM, 0, 0);

        mPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                view.setVisibility(View.GONE);
                if (null != mPopup) {
                    mPopup.dismiss();
                    if (!isFrg) {
                        hideSoft(mRootView);
                    }
                }
            }
        });
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) mRootView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 10);
    }

    /**
     * 隐藏键盘
     */
    public void hideSoft(final View v) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }, 10);
    }

    /**
     * 弹出黏贴框
     */
    public void showCopyDialog() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.textview_copy, null);
        ((TextView) view.findViewById(R.id.id_niantie_txt)).setText("粘贴");
        final Dialog mDialog = DialogUtils.selfDialog(mContext, view, true);
        mDialog.show();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
                // 得到剪贴板管理器
                ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
                CharSequence text1 = cmb.getText();
                if (null != text1) {
                    String text2 = text1.toString().trim();
                    String text = mEtvComment.getText().toString().trim();
                    if (StringUtils.isEmpty(text2)) {
                        if (!StringUtils.isEmpty(text)) {
                            mEtvComment.setSelection(text.length());
                        }
                    } else {
                        mEtvComment.setText(text + text2);
                        mEtvComment.setSelection((text + text2).length());
                    }
                }
            }
        });
    }

    @Override
    public void detachView() {
    }

    @Override
    public List<CheerCircleModel> getCacheData() {
        return mCheerCircleCache.getListEntity(CheerCircleModel.class);
    }
}
